import {
    query, save, update, get, remove, doInvoice, exportInvoice, recInvoice, updateOrder
} from '../../services/drawback/tax';
import { message } from 'antd';
import Constants from '../../utils/Constants'

const R = require('ramda');

export default {
    namespace: 'tax',
    state: {
        page: {},
        list: [],
        dataSource: {}
    },
    reducers: {
        refreshState(state, { payload }) {
            return { ...state, ...payload };
        }
    },
    effects: {

        *query({ payload }, { call, put }) {
            const res = yield call(query, { ...R.omit(['onSuccess', 'onFail'])(payload) });
            if (res && res.data.resultCode === "ok") {
                yield put({
                    type: 'refreshState',
                    payload: {
                        list: res.data.content.list ? res.data.content.list : [],
                        page: res.data.content.page ? res.data.content.page : Constants.DEFAULT_PAGE
                    }
                });
            } else {
                message.error(res.data.errMessage);
                if (payload.onFail) payload.onFail();
            }
        },

        *get({ payload }, { call, put }) {
            if (payload.id) {
                const res = yield call(get, { ...R.omit(['onSuccess', 'onFail'])(payload) });
                if (res && res.data.resultCode === "ok") {
                    yield put({
                        type: 'refreshState',
                        payload: {
                            dataSource: res.data.content
                        }
                    });
                } else {
                    message.error(res.data.errMessage);
                    if (payload.onFail) payload.onFail();
                }
            } else {
                yield put({
                    type: 'refreshState',
                    payload: {
                        dataSource: {}
                    }
                });
            }
        },

        *save({ payload }, { call }) {
            const res = yield call(save, { ...R.omit(['onSuccess', 'onFail'])(payload) });
            if (res && res.data.resultCode === "ok") {
                message.info("新增成功！");
                if (payload.onSuccess) payload.onSuccess(res.data.content);
            } else {
                message.error(res.data.errMessage);
                if (payload.onFail) payload.onFail();
            }
        },

        *update({ payload }, { call, put }) {
            const res = yield call(update, { ...R.omit(['onSuccess', 'onFail'])(payload) });
            if (res && res.data.resultCode === "ok") {
                message.info("更新成功！");
                if (payload.onSuccess) payload.onSuccess();
            } else {
                message.error(res.data.errMessage);
                if (payload.onFail) payload.onFail();
            }
        },

        *remove({ payload }, { call }) {
            const res = yield call(remove, { ...R.omit(['onSuccess', 'onFail'])(payload) });
            if (res && res.data.resultCode === "ok") {
                message.info("删除成功！");
                if (payload.onSuccess) payload.onSuccess();
            } else {
                message.error(res.data.errMessage);
                if (payload.onFail) payload.onFail();
            }
        },
        *doInvoice({ payload }, { call, put }) {
            const res = yield call(doInvoice, { ...R.omit(['onSuccess', 'onFail'])(payload) });
            if (res && res.data.resultCode === "ok") {
                message.info("更新成功！");
                if (payload.onSuccess) payload.onSuccess();
            } else {
                message.error(res.data.errMessage);
                if (payload.onFail) payload.onFail();
            }
        },
        *recInvoice({ payload }, { call, put }) {
            const res = yield call(recInvoice, { ...R.omit(['onSuccess', 'onFail'])(payload) });
            if (res && res.data.resultCode === "ok") {
                message.info("操作成功！");
                if (payload.onSuccess) payload.onSuccess();
            } else {
                message.error(res.data.errMessage);
                if (payload.onFail) payload.onFail();
            }
        },
        *updateOrder({ payload }, { call, put }) {
            const res = yield call(updateOrder, { ...R.omit(['onSuccess', 'onFail'])(payload) });
            if (res && res.data.resultCode === "ok") {
                message.info("提交成功！");
                if (payload.onSuccess) payload.onSuccess();
            } else {
                message.error(res.data.errMessage);
                if (payload.onFail) payload.onFail();
            }
        },
    },
    subscriptions: {}
};